package com.ch04.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

/**
 * @ClassName MvcConfig
 * @Description MVC配置类
 * @Author Kx
 * @Date 2025/5/12 15:01
 * @Version 1.0
 */
// 标识为配置类
@Configuration
// 启用MVC注解
@EnableWebMvc
// 扫描指定包下的类
@ComponentScan(basePackages = "com.ch04")
public class MvcConfig implements WebMvcConfigurer {
    /**
     * @author: kx
     * @date: 2025/5/12 15:32
     * @param configurer : 配置默认servlet
     * @description: 启用默认servlet
    */
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        // 启用默认servlet
        configurer.enable();
    }

    /**
     * @author: kx
     * @date: 2025/5/12 20:20
     * @param registry : 格式化器注册
     * @description: 注册全局的日期转换器
    */
    /*@Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addFormatter(new DateFormatter("yyyy-MM-dd"));
    }*/

    /**
     * @author: kx
     * @date: 2025/5/12 15:33
     * @param registry : 视图解析器注册
     * @description: 内部资源视图解析器
    */
    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        // 实例化内部资源视图解析器
        InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
        // 设置前缀
        internalResourceViewResolver.setPrefix("/static/");
        // 设置后缀
        internalResourceViewResolver.setSuffix(".html");
        // 注册视图解析器
        registry.viewResolver(internalResourceViewResolver);
    }
}
